package com.grad.why.studentsystem.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.Teacher;
import com.grad.why.studentsystem.domain.Teacherposition;
import com.grad.why.studentsystem.domain.Workrole;
import com.grad.why.studentsystem.domain.current.CurrentTeacher;
import com.grad.why.studentsystem.mapper.TeacherMapper;
import com.grad.why.studentsystem.mapper.TeacherpositionMapper;
import com.grad.why.studentsystem.mapper.WorkroleMapper;
import com.grad.why.studentsystem.service.StudentarrivedinfoService;
import com.grad.why.studentsystem.service.TeacherService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Component
@Service(interfaceClass = TeacherService.class)
@Transactional
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private TeacherpositionMapper teacherpositionMapper;

    @Autowired
    private WorkroleMapper workroleMapper;


    @Override
    public List<CurrentTeacher> getTeacherList(int offset, int limit, String sort, String search) {
        try{
            return teacherMapper.getTeacherList(offset,limit,sort,search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getTeacherListLength(String search) {
        try{
            return teacherMapper.getTeacherListLength(search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteTeacherById(Integer s) {
        try{
            return teacherMapper.deleteByPrimaryKey(s);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Teacherposition> getTeacherPosition() {
        try{
            return teacherMapper.getTeacherPosition();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Workrole> getWorkRoles() {
        try{
            return teacherMapper.getWorkRoles();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer getMaxId() {
        try{
            return teacherMapper.getMaxId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Teacher saveTeacherByEntity(Teacher teacher) {
        try{
            int rows = teacherMapper.insertSelective(teacher);
            if(rows>0){
                return teacher;
            }else{
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateTeacherByEntity(Teacher teacher) {
        try{
            return teacherMapper.updateByPrimaryKey(teacher);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Teacher> getTeacherInfos() {
        try{
            return teacherMapper.getTeacherInfos();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Teacher getTeacherInfoByCourseId(int cid) {
        try{
            return teacherMapper.getTeacherInfoByCourseId(cid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addTeacherInfosByExcelFile(String uploadCurrentFileName) {
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            DecimalFormat decimalFormat = new DecimalFormat("##0");//格式化设置
            InputStream is = new FileInputStream(uploadCurrentFileName);
            Workbook wb = null;

            if(uploadCurrentFileName.split("\\.")[1].equals("xls")){
                wb = new HSSFWorkbook(is);
            }else if(uploadCurrentFileName.split("\\.")[1].equals("xlsx")){
                wb = new XSSFWorkbook(is);
            }
            int sheetNumber = wb.getNumberOfSheets();
            int InsertCount = 0;
            Row title = wb.getSheetAt(0).getRow(0);
            if(!title.getCell(0).getStringCellValue().equals("姓名")||
                    !title.getCell(1).getStringCellValue().equals("性别")||
                    !title.getCell(2).getStringCellValue().equals("年龄")||
                    !title.getCell(3).getStringCellValue().equals("邮箱")||
                    !title.getCell(4).getStringCellValue().equals("出生日期")||
                    !title.getCell(5).getStringCellValue().equals("民族")||
                    !title.getCell(6).getStringCellValue().equals("职称")||
                    !title.getCell(7).getStringCellValue().equals("电话")||
                    !title.getCell(8).getStringCellValue().equals("地址")||
                    !title.getCell(9).getStringCellValue().equals("职位")){
                return 0;
            }

            for(int i = 0;i<sheetNumber;i++) {
                int index = 1;
                for(Row row : wb.getSheetAt(i)){
                    if(row == null){
                        continue;
                    }else{
                        if(index == 1){

                        }else{
                            if(row.getCell(0) == null){
                                index++;
                                continue;
                            }else{
                                Teacher teacher = new Teacher();
                                teacher.setId(teacherMapper.getMaxId() + 1);
                                teacher.setName(row.getCell(0).getStringCellValue());
                                teacher.setSex(row.getCell(1).getStringCellValue());
                                teacher.setAge((int)row.getCell(2).getNumericCellValue());
                                teacher.setEmail(row.getCell(3).getStringCellValue());
                                teacher.setBirthday(sdf.parse(row.getCell(4).getStringCellValue()));
                                teacher.setNation(row.getCell(5).getStringCellValue());
                                int positionId = teacherpositionMapper.getPositionIdByPositionName(row.getCell(6).getStringCellValue());
                                teacher.setPosition(positionId);
                                teacher.setTel(decimalFormat.format(row.getCell(7).getNumericCellValue()));
                                teacher.setAddress(row.getCell(8).getStringCellValue());
                                int workRoleId = workroleMapper.getworkRoleIdByWorkRoleName(row.getCell(9).getStringCellValue());
                                teacher.setWorkrole(workRoleId);

                                teacherMapper.insertSelective(teacher);
                                InsertCount = InsertCount + 1;
                            }
                        }
                        index++;
                    }
                }
            }
            return InsertCount;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Teacher getTeacherInfoByTid(int bindid) {
        try{
            return teacherMapper.selectByPrimaryKey(bindid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


}
